/*
 * File: ambientLightCtrl.c
 *
 * Code generated for Simulink model 'ambientLightCtrl'.
 *
 * Model version                  : 1.5
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Wed May  8 14:54:22 2024
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "ambientLightCtrl.h"
#include "ambientLightCtrl_private.h"

/* Block states (default storage) */
DW_ambientLightCtrl_T ambientLightCtrl_DW;

/* External inputs (root inport signals with default storage) */
ExtU_ambientLightCtrl_T ambientLightCtrl_U;

/* External outputs (root outports fed by signals with default storage) */
ExtY_ambientLightCtrl_T ambientLightCtrl_Y;

/* Real-time model */
static RT_MODEL_ambientLightCtrl_T ambientLightCtrl_M_;
RT_MODEL_ambientLightCtrl_T *const ambientLightCtrl_M = &ambientLightCtrl_M_;

/*
 * System reset for enable system:
 *    '<S1>/Subsystem'
 *    '<S1>/Subsystem1'
 *    '<S1>/Subsystem2'
 */
void ambientLightCtr_Subsystem_Reset(DW_Subsystem_ambientLightCtrl_T *localDW)
{
  /* InitializeConditions for Delay: '<S2>/Delay' */
  localDW->Delay_DSTATE = 0.0;
}

/*
 * Disable for enable system:
 *    '<S1>/Subsystem'
 *    '<S1>/Subsystem1'
 *    '<S1>/Subsystem2'
 */
void ambientLightC_Subsystem_Disable(DW_Subsystem_ambientLightCtrl_T *localDW)
{
  localDW->Subsystem_MODE = false;
}

/*
 * Output and update for enable system:
 *    '<S1>/Subsystem'
 *    '<S1>/Subsystem1'
 *    '<S1>/Subsystem2'
 */
void ambientLightCtrl_Subsystem(real_T rtu_Enable, real_T *rty_Output,
  DW_Subsystem_ambientLightCtrl_T *localDW)
{
  /* Outputs for Enabled SubSystem: '<S1>/Subsystem' incorporates:
   *  EnablePort: '<S2>/Enable'
   */
  if (rtu_Enable > 0.0) {
    if (!localDW->Subsystem_MODE) {
      ambientLightCtr_Subsystem_Reset(localDW);
      localDW->Subsystem_MODE = true;
    }

    /* Sum: '<S2>/Add' incorporates:
     *  Constant: '<S2>/Constant'
     *  Delay: '<S2>/Delay'
     */
    *rty_Output = localDW->Delay_DSTATE + 1.0;

    /* Update for Delay: '<S2>/Delay' */
    localDW->Delay_DSTATE = *rty_Output;
  } else {
    if (localDW->Subsystem_MODE) {
      ambientLightC_Subsystem_Disable(localDW);
    }
  }

  /* End of Outputs for SubSystem: '<S1>/Subsystem' */
}

/* Model step function */
void ambientLightCtrl_step(void)
{
  int32_T rtb_PulseGenerator;
  int_T idxDelay;

  /* Outputs for Enabled SubSystem: '<S1>/Subsystem2' */
  /* Delay: '<S1>/Delay1' incorporates:
   *  Outport: '<Root>/ ambient_blue'
   */
  ambientLightCtrl_Subsystem(ambientLightCtrl_DW.Delay1_DSTATE[0],
    &ambientLightCtrl_Y.ambient_blue, &ambientLightCtrl_DW.Subsystem2);

  /* End of Outputs for SubSystem: '<S1>/Subsystem2' */

  /* Outputs for Enabled SubSystem: '<S1>/Subsystem1' */
  /* Delay: '<S1>/Delay' incorporates:
   *  Outport: '<Root>/ ambient_green'
   */
  ambientLightCtrl_Subsystem(ambientLightCtrl_DW.Delay_DSTATE[0],
    &ambientLightCtrl_Y.ambient_green, &ambientLightCtrl_DW.Subsystem1);

  /* End of Outputs for SubSystem: '<S1>/Subsystem1' */

  /* DiscretePulseGenerator: '<S1>/Pulse Generator' */
  rtb_PulseGenerator = ((ambientLightCtrl_DW.clockTickCounter < 250) &&
                        (ambientLightCtrl_DW.clockTickCounter >= 0));
  if (ambientLightCtrl_DW.clockTickCounter >= 499) {
    ambientLightCtrl_DW.clockTickCounter = 0;
  } else {
    ambientLightCtrl_DW.clockTickCounter++;
  }

  /* End of DiscretePulseGenerator: '<S1>/Pulse Generator' */

  /* Switch: '<S1>/Switch' incorporates:
   *  Inport: '<Root>/sw_pulse'
   */
  if (ambientLightCtrl_U.sw_pulse > 0.0) {
    /* Outport: '<Root>/  pulse_signal' */
    ambientLightCtrl_Y.pulse_signal = rtb_PulseGenerator;
  } else {
    /* Outport: '<Root>/  pulse_signal' incorporates:
     *  Constant: '<S1>/Constant'
     */
    ambientLightCtrl_Y.pulse_signal = 0.0;
  }

  /* End of Switch: '<S1>/Switch' */

  /* Outputs for Enabled SubSystem: '<S1>/Subsystem' */
  /* Outport: '<Root>/ ambient_red' */
  ambientLightCtrl_Subsystem((real_T)rtb_PulseGenerator,
    &ambientLightCtrl_Y.ambient_red, &ambientLightCtrl_DW.Subsystem);

  /* End of Outputs for SubSystem: '<S1>/Subsystem' */

  /* Update for Delay: '<S1>/Delay1' incorporates:
   *  Delay: '<S1>/Delay'
   */
  for (idxDelay = 0; idxDelay < 99; idxDelay++) {
    ambientLightCtrl_DW.Delay1_DSTATE[idxDelay] =
      ambientLightCtrl_DW.Delay1_DSTATE[idxDelay + 1];
  }

  ambientLightCtrl_DW.Delay1_DSTATE[99] = ambientLightCtrl_DW.Delay_DSTATE[0];

  /* End of Update for Delay: '<S1>/Delay1' */

  /* Update for Delay: '<S1>/Delay' */
  for (idxDelay = 0; idxDelay < 99; idxDelay++) {
    ambientLightCtrl_DW.Delay_DSTATE[idxDelay] =
      ambientLightCtrl_DW.Delay_DSTATE[idxDelay + 1];
  }

  ambientLightCtrl_DW.Delay_DSTATE[99] = rtb_PulseGenerator;

  /* End of Update for Delay: '<S1>/Delay' */
}

/* Model initialize function */
void ambientLightCtrl_initialize(void)
{
  /* (no initialization code required) */
}

/* Model terminate function */
void ambientLightCtrl_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
